package com.uls.web.controller.hdh;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.uls.hdh.domain.*;
import com.uls.hdh.service.IHdhOptionsService;
import com.uls.hdh.service.IHdhPollsService;
import com.uls.hdh.service.IHdhUsersService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.hdh.service.IHdhVotesService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 投票记录Controller
 * 
 * @author hdh
 * @date 2024-11-11
 */
@RestController
@RequestMapping("/hdh/votes")
public class HdhVotesController extends BaseController
{
    @Autowired
    private IHdhVotesService hdhVotesService;
    @Autowired
    private IHdhOptionsService hdhOptionsService;

    @Autowired
    private IHdhPollsService hdhPollsService;
    @Autowired
    private IHdhUsersService hdhUsersService;
    /**
     * 查询投票记录列表
     */
    @PreAuthorize("@ss.hasPermi('hdh:votes:list')")
    @GetMapping("/list")
    public TableDataInfo list(HdhVotes hdhVotes)
    {
        startPage();
        List<HdhVotes> list = hdhVotesService.selectHdhVotesList(hdhVotes);


        for (HdhVotes hdhVotes1:list){
            Long usersId=hdhVotes1.getUsersId();
            HdhUsers hdhUsers=hdhUsersService.selectHdhUsersByUsersId(usersId);
            if(hdhUsers!=null){
                hdhVotes1.setUsername(hdhUsers.getUsername());
            }
        }
        for (HdhVotes hdhVotes1:list){
            Long pollId=hdhVotes1.getPollId();
            HdhPolls hdhPolls=hdhPollsService.selectHdhPollsByPollId(pollId);
            if(hdhPolls!=null){
                hdhVotes1.setTitle(hdhPolls.getTitle());
            }
        }
        for (HdhVotes hdhVotes1:list){
            Long optionId=hdhVotes1.getOptionId();
            HdhOptions hdhOptions=hdhOptionsService.selectHdhOptionsByOptionId(optionId);
            if(hdhOptions!=null){
                hdhVotes1.setOptionText(hdhOptions.getOptionText());
            }
        }


        return getDataTable(list);
    }

    /**
     * 导出投票记录列表
     */
    @PreAuthorize("@ss.hasPermi('hdh:votes:export')")
    @Log(title = "投票记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HdhVotes hdhVotes)
    {
        List<HdhVotes> list = hdhVotesService.selectHdhVotesList(hdhVotes);
        ExcelUtil<HdhVotes> util = new ExcelUtil<HdhVotes>(HdhVotes.class);
        util.exportExcel(response, list, "投票记录数据");
    }

    /**
     * 获取投票记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('hdh:votes:query')")
    @GetMapping(value = "/{voteId}")
    public AjaxResult getInfo(@PathVariable("voteId") Long voteId)
    {
        return success(hdhVotesService.selectHdhVotesByVoteId(voteId));
    }

    /**
     * 新增投票记录
     */
    @PreAuthorize("@ss.hasPermi('hdh:votes:add')")
    @Log(title = "投票记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HdhVotes hdhVotes)
    {
        return toAjax(hdhVotesService.insertHdhVotes(hdhVotes));
    }

    /**
     * 修改投票记录
     */
    @PreAuthorize("@ss.hasPermi('hdh:votes:edit')")
    @Log(title = "投票记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HdhVotes hdhVotes)
    {
        return toAjax(hdhVotesService.updateHdhVotes(hdhVotes));
    }

    /**
     * 删除投票记录
     */
    @PreAuthorize("@ss.hasPermi('hdh:votes:remove')")
    @Log(title = "投票记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{voteIds}")
    public AjaxResult remove(@PathVariable Long[] voteIds)
    {
        return toAjax(hdhVotesService.deleteHdhVotesByVoteIds(voteIds));
    }
}
